﻿var CSPLeftCategory = {
    Data: undefined, // {[{Id:1,Name:"培训专区",OrderId:1,ParentId:1,Level:1}]}
    AreaData: undefined,
    AgeData: undefined,
    Init: function () {
        this.GetCategoryInfo();
        this.CreateFirstLevelHtml();
        var firstLevel = $("#CSPDomainType");
        firstLevel.find("li").find("a").eq(0).click();
    },
    CreateFirstLevelHtml: function () {
        var temData = [];
        var i = 0;
        var firstLevel = $("#CSPDomainType");

        //获取出第一级别的数据
        $.each(this.Data, function (index, data) {
            if (data.Level == 1) {
                temData[i] = data;
                i++;
            }
        });
        //对第一级别进行排序
        temData.sort(function (a, b) {
            return a.OrderId - b.OrderId;
        });
        //生成第一级别的分类
        $.each(temData, function (index, data) {
            var temli = ""
            if (index == 0) {
                temli = $('<li class="field' + (index + 1) + '"><a class="current" onclick="CSPLeftCategory.CreateSecondLevelHtml(this,' + data.Id + ');" >培训专区</a></li>');
            }
            else {
                temli = $('<li class="field' + (index + 1) + '"><a  href="javascript:CSPLeftCategory.CreateSecondLevelHtml(this,' + data.Id + ');">培训专区</a></li>');
            }
            temli.find("a").bind("mouseenter", function () {
                CSPLeftCategory.CreateSecondLevelHtml(this, data.Id);
            });
            firstLevel.append(temli);
        });
    },
    //创建第二级别分类
    CreateSecondLevelHtml: function (firstLeveObj, parentId) {
        var that = this;
        this.FirstLeveSelectStatus(firstLeveObj);

        var temData = [];
        var i = 0;
        var j = 0;
        var SecondLevel = $(".category-list");

        //获取出第二级别的数据
        $.each(this.Data, function (index, data) {
            if (data.Level == 2 && data.ParentId == parentId) {
                temData[i] = data;
                i++;
            }
        });

        //对第二级别进行排序
        temData.sort(function (a, b) {
            return a.OrderId - b.OrderId;
        });

        //生成第二级别的分类
        $.each(temData, function (index, data) {
            var temItem = '<dl class="cl-item"><a href="/search/st' + data.Id + '/" target="_blank"><dt>' + data.Name + '<i class="navicon"></i></dt></a>';
            temItem += '<dd>';
            var threeLeveData = that.GetThreeLeveData(data.Id);
            j = 0;
            $.each(threeLeveData, function (threeLeveIndex, temThreeLeveData) {
                if (j < 6) {
                    temItem += '<a href="/search/st' + temThreeLeveData.Id + '/" target="_blank">' + temThreeLeveData.Name + '</a>';
                }
                j++;
            });
            temItem += '</dd><dd></dd></dl>';
            temItem = $(temItem);
            temItem.bind("mouseenter", function () {

                CSPLeftCategory.CreateThirdLevelHtml(this, data.Id);
            });
            temItem.bind("mouseleave", function () {

                temItem.find("dd").eq(1).find("div").eq(0).hide();
            });
            SecondLevel.append(temItem);
        });

    },
    //创建第三级别分类
    CreateThirdLevelHtml: function (firstLeveObj, parentId) {
        var that = this;
        //第三级别
        var threeLevel = $(".category-list");

        //将所有第三级别分类隐藏
        var cl_items = $(".category-list").find(".cl-item");
        $.each(cl_items, function (index, data) {
            var tem_dd = $(data).find("dd").eq(1);
            if (tem_dd.html() != "") {
                tem_dd.find("div").eq(0).hide();
            }


        });

        var current_dd = $(firstLeveObj).find("dd").eq(1);
        //生成第三级别分类
        if (current_dd.html() == "") {
            var pop_subcategory = '<div class="pop-subcategory"><div class="shadow"><div class="list-item hidden" style="display: block;">';
            var temData = that.GetThreeLeveData(parentId);
            pop_subcategory += '<div class="lst-subcategory"> <dl class="first"><dt><a>分类</a></dt><dd>';
            $.each(temData, function (index, categoryData) {
                pop_subcategory += '<a href="/search/st' + categoryData.Id + '/" target="_blank">' + categoryData.Name + '</a>';
            });
            pop_subcategory += '</dd></dl>';

            //生成地区
            pop_subcategory += '<dl><dt><a>区域</a></dt><dd>';
            $.each(this.AreaData, function (index, areaData) {
                pop_subcategory += '<a href="/search/st' + parentId + '-sa' + areaData.Id + '/" target="_blank">' + areaData.Name + '</a>';
            });
            pop_subcategory += '</dd></dl>';

            pop_subcategory += '<dl><dt><a>年龄段</a></dt><dd>';
            //生成年龄段
            $.each(this.AgeData, function (index, ageData) {
                pop_subcategory += '<a href="/search/st' + parentId + '-a' + ageData.Id + '/" target="_blank">' + ageData.Name + '</a>';
            });
            pop_subcategory += '</dd></dl></div>';

            //生成店铺课程
            pop_subcategory += '<div class="lst-top"><div class="lst-topitems"><dl class="first"><dt><a>推荐课程<b>»</b></a> </dt><dd>';
            pop_subcategory += '</dd></div></div>';

            pop_subcategory += "</div></div></div>"

            pop_subcategory = $(pop_subcategory)
            current_dd.append(pop_subcategory);
            current_dd.find("div").eq(0).bind("mouseleave", function () {
                $(this).hide();
            });
        }
        current_dd.find("div").eq(0).show();
    },
    //更新第一级别选中的状态
    FirstLeveSelectStatus: function (firstLeveObj) {
        $("#CSPDomainType").find("a").attr("class", "");
        $(firstLeveObj).attr("class", "current");
        var totalClass = $(firstLeveObj).parent().attr("class");
        totalClass = totalClass.replace("field", "field-cont")
        $(".category-list").html("");
        $(".list-warp").attr("class", "list-warp " + totalClass);

    },
    //获取第三级别数据
    GetThreeLeveData: function (parentId) {
        var result = [];
        var i = 0;
        //获取匹配的第三级别数据
        $.each(this.Data, function (index, data) {
            if (data.Level == 3 && data.ParentId == parentId) {
                result[i] = data;
                i++;
            }
        });

        result.sort(function (a, b) {
            return a.OrderId - b.OrderId;
        });
        return result;
    },
    GetCategoryInfo: function () {
        var that = this;
        $.ajax({
            dataType: "json",
            type: "get",
            async: false,
            url: window.location.pathname + "?getcategoryinfo=1",
            success: function (data, textStatus, jqXHR) {
                that.Data = data.ServiceType;
                that.AreaData = data.ServiceArea;
                that.AgeData = data.AgeGroup;
            },
            error: function (result) {
                result;
            }
        });
    }
};

    